package com.jzy.util;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 获取微信网页里的封面图片和标题
 *
 * @author jzy
 * @create 2017-03-17 15:16
 **/
public class GetHttpUtils {

    // 地址
    public static final String URL = "http://mp.weixin.qq.com/s?__biz=MjM5MzY0NDI4OA==&mid=2650563784&idx=1&sn=dbc2344d4b0c6785d481fd1cc05819ad&chksm=be9b2ea789eca7b10ea98e7a7799e904bc4028f299483ba5b1fe809accf05b1bf856e58879b7&scene=0#rd";
    // 编码
    public static final String ECODING = "UTF-8";
    // 获取img标签正则
    public static final String IMGURL_REG = "var msg_cdn_url.*";
    // 获取src路径的正则
    public static final String IMGSRC_REG = "http:\"?(.*?)(\"|>|\\s+)";
    //获取标题的正则
    public static final String TITLE_REG = "var msg_title.*";


    public static void main(String[] args) throws Exception {
        //获得html文本内容
        String HTML = getHTML(URL);
        //获取图片标签
        List<String> imgUrl =getImageUrl(HTML);
        System.out.println("地址是:"+imgUrl);
        //获取图片src地址
        List<String> imgSrc = getImageSrc(imgUrl);
        System.out.println("最终的地址是:"+imgSrc);
        //获取文章标题
        String title  = getTitle(HTML);
        System.out.println("标题是:"+title);
    }

    /***
     * 获取HTML内容
     *
     * @param url
     * @return
     * @throws Exception
     */
    public static String getHTML(String url) throws Exception {
        URL uri = new URL(url);
        URLConnection connection = uri.openConnection();
        InputStream in = connection.getInputStream();
        byte[] buf = new byte[1024];
        int length = 0;
        StringBuffer sb = new StringBuffer();
        while ((length = in.read(buf, 0, buf.length)) > 0) {
            sb.append(new String(buf, ECODING));
        }
        in.close();
        return sb.toString();
    }

    /***
     * 获取ImageUrl地址
     *
     * @param HTML
     * @return
     */
    public static List<String> getImageUrl(String HTML) {
        Matcher matcher = Pattern.compile(IMGURL_REG).matcher(HTML);
        List<String> listImgUrl = new ArrayList<String>();
        while (matcher.find()) {
            listImgUrl.add(matcher.group());
        }
        return listImgUrl;
    }
    /***
     * 获取ImageSrc地址
     *
     * @param listImageUrl
     * @return
     */
    public static List<String> getImageSrc(List<String> listImageUrl) {
        List<String> listImgSrc = new ArrayList<String>();
        for (String image : listImageUrl) {
            Matcher matcher = Pattern.compile(IMGSRC_REG).matcher(image);
            while (matcher.find()) {
                listImgSrc.add(matcher.group().substring(0, matcher.group().length() - 1));
            }
        }
        return listImgSrc;
    }

    /**
     * 获取标题方法
     * @param HTML
     * @return 标题
     */
    public static String getTitle(String HTML) {
        Matcher matcher = Pattern.compile(TITLE_REG).matcher(HTML);
       String title = "";
        while (matcher.find()) {
            title = matcher.group().substring(15);
        }
        return title;
    }

}
